package 第二十章项目.clothesSystem.utils;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.Xpp3Driver;
import org.junit.Test;
import 第二十章项目.clothesSystem.ShellOfBean.ShellofClothes;
import 第二十章项目.clothesSystem.bean.Clothes;
import 第二十章项目.clothesSystem.bean.Order;
import 第二十章项目.clothesSystem.bean.OrderItem;
import 第二十章项目.clothesSystem.bean.User;


import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class XMLPaser {

    @Test
    public void xpp() {
        List<ShellofClothes> postlist=getpostlist();
        XStream xstream = new XStream(new Xpp3Driver());

        xstream.allowTypes(new Class[] {Clothes.class, Order.class, OrderItem.class, User.class, ShellofClothes.class});
        xstream.alias("各组品牌统计表", ShellofClothes.class);
        xstream.alias("各品牌产品",Clothes.class);
        xstream.useAttributeFor(Clothes.class,"id");
        xstream.addImplicitCollection(ShellofClothes.class,"clotheslist");
        //xstream.aliasField("Product",Clothes.class, "list");
        try {
            BufferedOutputStream outputStream = null;
            BufferedInputStream inputStream=null;
            File file=new File("."+"\\"+"products.xml");
            if(!file.exists()) {
                outputStream=new BufferedOutputStream(
                        new FileOutputStream(file,true));
                outputStream.write("<?xml version=\"1.0\" encoding=\"utf-8\" ?>".getBytes());
                xstream.toXML(postlist, outputStream);
                outputStream.close();
                System.out.println("写入完毕!");
            }else{
                System.out.println("文件已经存在,路径是: "+file.getAbsolutePath());
            }
            if(file.exists()) {
                inputStream = new BufferedInputStream(new FileInputStream(file));
                final Object o = xstream.fromXML(inputStream);
                List<ShellofClothes>product=(List<ShellofClothes>)o;
                System.out.println("文件读取开始: ");
                product.stream().forEach(System.out::println);
                System.out.println("文件读取结束! ");
            }else{
                System.out.println("文件不存在.");
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        //下2句是直接读取编写的xml标签 没有写文件
        //String string_postlist_xml=xstream.toXML(postlist);
        //System.out.println(string_postlist_xml);

        //下2句是直接读取xml标签  没写文件
        //List<ShellofClothes> xml_postlist_json = (List<ShellofClothes>)xstream.fromXML(string_postlist_xml);
        //xml_postlist_json.stream().forEach(System.out::println);
    }

    public static List<ShellofClothes> getpostlist(){
        List<ShellofClothes>paser_list=new ArrayList<>();
        List<ShellofClothes>total=new ArrayList<>();
        ShellofClothes totalcount=new ShellofClothes("null");
        ShellofClothes group1=new ShellofClothes();
        ShellofClothes group2=new ShellofClothes();
        StringBuffer sb=new StringBuffer();
        group1.addclothes_obj(new Clothes("A001","Hermes","连体皮衣","棕色","L",42,2274.12,"2023夏季爆款"));
        group1.addclothes_obj(new Clothes("A002","Louis Vuitton","挎肩包","淡绿色","S",4,1274.99,"2023秋季引流款"));
        group1.addclothes_obj(new Clothes("A003","Prada","长裤","浅白色","S",45,1999.99,"2023夏级畅销"));
        group1.addclothes_obj(new Clothes("A004","Chanel","香水","淡黄色","M",74,3274.02,"2023夏季流行款"));
        group1.addclothes_obj(new Clothes("A005","Dior","眉毛刷","浅红色","S",13,6274.01,"2023冬季引流款"));
        group1.setName("2023年度");
        group1.setCount(group1.clotheslist_obj_count());
        group1.setMoney(group1.clotheslist_obj_money());
        group2.addclothes_obj(new Clothes("A006","Gucci","手链","银白色","S",2,8276.06,"2024夏季盈利款"));
        group2.addclothes_obj(new Clothes("A007","Armani","连衣裙","花边色","M",9,2374.04,"2024夏季爆款"));
        group2.addclothes_obj(new Clothes("A008","Versace","T恤衫","彩黄色","S",67,3212.44,"2024夏季流行款"));
        group2.addclothes_obj(new Clothes("A009","Burberry","女士长裤","藏青色","L",12,7974.67,"2024夏季盈利款"));
        group2.addclothes_obj(new Clothes("A010","Givenchy","男士风衣","咖啡色","S",31,2334.23,"2024春季流行款"));
        group2.setName("2024年度");
        group2.setCount(group2.clotheslist_obj_count());
        group2.setMoney(group2.clotheslist_obj_money());
        paser_list.add(group1);
        paser_list.add(group2);
        sb.append(Double.parseDouble(String.format("%.2f", paser_list.stream().mapToDouble(ShellofClothes::getMoney).sum())) + "元")
                .append(paser_list.stream().mapToDouble(ShellofClothes::getCount).sum()+"种");
        totalcount.setTotalinfo(String.valueOf(sb));
        totalcount.setName("totalcount");
        paser_list.add(totalcount);
        //下面是把最后一个放置到第一个，其他依序排序
        final int index = paser_list.indexOf(totalcount);
        if(index!=-1){
            Collections.rotate(paser_list.subList(0,index+1),-index);
        }
        //paser_list.stream().forEach(System.out::println);
        return paser_list;
    }
}
